package com.lxq.live.security;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class JWTLoginFilter extends UsernamePasswordAuthenticationFilter {
    @Override
    public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException {
        var userId = request.getParameter("userId");
        var password = request.getParameter("password");
        /*var jwtToken=new JwtLoginToken(userId,password);
        jwtToken.setDetails(new WebAuthenticationDetails(request));*/
        System.out.println("JWTLoginFilter attemptAuthentication");
        var jwtToken = new UsernamePasswordAuthenticationToken(userId, password);
        return getAuthenticationManager().authenticate(jwtToken);
    }

    @Override
    public void setAuthenticationManager(AuthenticationManager authenticationManager) {
        super.setAuthenticationManager(authenticationManager);
    }
}